package borrower

import (
	"NovelBook/logic/book"
	"NovelBook/models/res"
	"NovelBook/utils/auth"
	"NovelBook/utils/response"
	"net/http"

	"github.com/gin-gonic/gin"
)

// 借书要确定用户借走的是具体的哪一本书，所以只能通过 book_unique_id 来借书

func BorrowBooks(c *gin.Context) {
	idAny, exist := c.Get(auth.UserIDKey)
	id := idAny.(uint)
	if !exist || id < 1 {
		response.Fail(c, http.StatusUnauthorized, response.CodeTokenInvalid)
		return
	}

	var req res.BookUniqueID

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
		return
	}

	// 借书操作
	err := book.BorrowBooksByUniqueID(id, req.BookUniqueIDs)
	if err != nil {
		response.FailWithMsg(c, http.StatusInternalServerError, response.CodeSystemBusy, err.Error())
		return
	}

	response.Success(c, nil)
}
